package com.example.menhoo.toollibrary.common.assist;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.IBinder;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

/**
 * 弹出隐藏 软键盘
 */
public class IMEHelper {
    /**
     * 显示或隐藏IME
     *
     * @param context
     * @param bHide true 是隐藏  false是显示
     */
    public static void hideIME(Activity context, boolean bHide) {
        if (bHide) {
            try {
                ((InputMethodManager) context
                        .getSystemService(Activity.INPUT_METHOD_SERVICE))
                        .hideSoftInputFromWindow(context.getCurrentFocus()
                                        .getWindowToken(),
                                InputMethodManager.HIDE_NOT_ALWAYS);
            } catch (NullPointerException npe) {
            }
        } else { // show IME
            try {
                ((InputMethodManager) context
                        .getSystemService(Activity.INPUT_METHOD_SERVICE))
                        .showSoftInput(context.getCurrentFocus(),
                                InputMethodManager.SHOW_IMPLICIT);
            } catch (NullPointerException npe) {
            }
        }
    }
    /**
     * 显示或隐藏IME
     */
    public static void hideIME(Activity context,IBinder token, boolean bHide) {
        if (bHide) {
            try {
                ((InputMethodManager) context
                        .getSystemService(Activity.INPUT_METHOD_SERVICE))
                        .hideSoftInputFromWindow(token,
                                InputMethodManager.HIDE_NOT_ALWAYS);
            } catch (NullPointerException npe) {
            }
        } else { // show IME
            try {
                ((InputMethodManager) context
                        .getSystemService(Activity.INPUT_METHOD_SERVICE))
                        .showSoftInput(context.getCurrentFocus(),
                                InputMethodManager.SHOW_IMPLICIT);
            } catch (NullPointerException npe) {
            }
        }
    }

    /**
     * 在dialog开启前确定需要开启后跳出IME
     *
     * @param dialog
     */
    public static void showIMEonDialog(AlertDialog dialog) {
        try {
            Window window = dialog.getWindow();
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        } catch (Exception e) {
        }
    }
}
